using Engine;
using Game;

namespace SCIENEW {
    /// <summary>
    /// 合成类机器弹出产物的电子元件控制类
    /// </summary>
    public class CrafterPopResultsElectricElement : ElectricElement {
        public bool m_isDispenseAllowed = true;

        public double? m_lastDispenseTime;

        public SubsystemBlockEntities m_subsystemBlockEntities;

        public SubsystemPickables m_subsystemPickables;

        public Vector3 m_position;

        public int m_direction;

        public CrafterPopResultsElectricElement(SubsystemElectricity subsystemElectricity, Point3 point, int direction) : base(
            subsystemElectricity,
            new List<CellFace> {
                new(point.X, point.Y, point.Z, 0),
                new(point.X, point.Y, point.Z, 1),
                new(point.X, point.Y, point.Z, 2),
                new(point.X, point.Y, point.Z, 3),
                new(point.X, point.Y, point.Z, 4),
                new(point.X, point.Y, point.Z, 5)
            }
        ) {
            m_subsystemBlockEntities = SubsystemElectricity.Project.FindSubsystem<SubsystemBlockEntities>(throwOnError: true);
            m_subsystemPickables = SubsystemElectricity.Project.FindSubsystem<SubsystemPickables>(true);
            m_position = new Vector3(point) + new Vector3(0.5f);
            m_direction = direction;
        }

        public override bool Simulate() {
            if (CalculateHighInputsCount() > 0) {
                if (m_isDispenseAllowed && (!m_lastDispenseTime.HasValue || SubsystemElectricity.SubsystemTime.GameTime - m_lastDispenseTime > 0.1)) {
                    m_isDispenseAllowed = false;
                    m_lastDispenseTime = SubsystemElectricity.SubsystemTime.GameTime;
                    ICrafterComponent? crafterComponent = m_subsystemBlockEntities.GetBlockEntity(CellFaces[0].Point.X, CellFaces[0].Point.Y, CellFaces[0].Point.Z)?.Entity.FindComponent<ICrafterComponent>();
                    if (crafterComponent is IInventory inventory) {
                        //先丢弃主产物
                        int resultValue = inventory.GetSlotValue(crafterComponent.ResultSlotIndex);
                        int resultCount = inventory.RemoveSlotItems(crafterComponent.ResultSlotIndex, inventory.GetSlotCount(crafterComponent.ResultSlotIndex));
                        if (resultCount > 0)
                            m_subsystemPickables.AddPickable(
                                resultValue,
                                resultCount,
                                m_position + CellFace.FaceToVector3(m_direction) * 0.7f,
                                CellFace.FaceToVector3(m_direction) * 4,
                                null
                            );
                        //再丢弃副产物
                        try {
                            int remainValue = inventory.GetSlotValue(crafterComponent.RemainsSlotIndex);
                            int remainCount = inventory.RemoveSlotItems(crafterComponent.RemainsSlotIndex, inventory.GetSlotCount(crafterComponent.RemainsSlotIndex));
                            if (remainCount > 0)
                                m_subsystemPickables.AddPickable(
                                    remainValue,
                                    remainCount,
                                    m_position + CellFace.FaceToVector3(m_direction) * 0.7f,
                                    CellFace.FaceToVector3(m_direction) * 4,
                                    null
                                );
                        }
                        catch (NotImplementedException) {
                        }
                    }
                }
            }
            else {
                m_isDispenseAllowed = true;
            }
            return false;
        }
    }
}